package cn.stronglink.assetmanage.business.patrol.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Patrol;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.listener.onRecyclerListener;

public class PatrolListAlreadyAdapter extends RecyclerView.Adapter<PatrolListAlreadyAdapter.ViewHolder>{

    private onRecyclerListener mListener;
    private Context mContext;
    private List<Patrol> patrolList;

    private LayoutInflater mLayoutInflater;

    public PatrolListAlreadyAdapter(Context context, List<Patrol> patrolList){
        this.mContext = context;
        this.patrolList = patrolList;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    public void setListener(onRecyclerListener listener){
        mListener = listener;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_patrol_list_already, parent, false);
        ViewHolder viewHolder = new ViewHolder(view,mListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Patrol patrol = patrolList.get(position);
        holder.bindBean(patrol);
        if (position%2!=0){
            holder.item_patrol.setBackgroundColor(mContext.getResources().getColor(R.color.gray_check_item));
        }else {
            holder.item_patrol.setBackgroundColor(Color.WHITE);

        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return patrolList == null ? 0 : patrolList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView tv_patrolNo,tv_patrolName,tv_patrolPlanTime,
                tv_patrolPerson,tv_patrolOverTime,tv_patrolUploadStatus;
        private onRecyclerListener mListener;
        private LinearLayout item_patrol;

        public ViewHolder(View itemView,onRecyclerListener listener) {
            super(itemView);
            tv_patrolNo = itemView.findViewById(R.id.item_patrol_no);
            tv_patrolName = itemView.findViewById(R.id.item_patrol_name);
            tv_patrolPlanTime = itemView.findViewById(R.id.item_patrol_plant_time);
            tv_patrolPerson = itemView.findViewById(R.id.item_patrol_person);
            tv_patrolOverTime = itemView.findViewById(R.id.item_patrol_overtime);
            tv_patrolUploadStatus = itemView.findViewById(R.id.item_patrol_upload_status);
            item_patrol = itemView.findViewById(R.id.item_patrol);
            itemView.setOnClickListener(this);
            this.mListener = listener;

        }

        public void bindBean(Patrol patrol){
            tv_patrolNo.setText(patrol.getId_());
            //实体类待改
            tv_patrolName.setText(patrol.getName());
            tv_patrolPlanTime.setText(patrol.getCreateName());
            tv_patrolPerson.setText(patrol.getOperatiorName());
            tv_patrolOverTime.setText(patrol.getUpdateTime());
            tv_patrolUploadStatus.setText("未上传");
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClicked(v, getAdapterPosition());
            }
        }
    }


    public void notifyDatasChanged(List<Patrol> data){
        this.patrolList = data;
        this.notifyDataSetChanged();
    }
}
